﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FriendStar.Code;
using FriendStar.Domain.Entity.MainData;
using FriendStar.Application.MainData;
using FriendStar.Domain.Entity.BOM;
using FriendStar.Application.BOM;

namespace FriendStar.Web.Areas.BOM.Controllers
{
    public class ProductController : ControllerBase
    {
        //
        // GET: /BOM/Product/
        private PLM_CP_VEHICLENUMBERApp plm_cp_vehiclenumberApp = new PLM_CP_VEHICLENUMBERApp();
        private V_PLM_GG_WGGGCPApp v_plm_gg_wgggcpApp = new V_PLM_GG_WGGGCPApp();
        private PLM_CP_WGCPApp plm_cp_wgcpApp = new PLM_CP_WGCPApp();
        private V_PLM_CP_WGGXApp v_plm_cp_wggxApp = new V_PLM_CP_WGGXApp();
        private V_PLM_CP_WGCP_GXApp v_plm_cp_wgcp_gxApp = new V_PLM_CP_WGCP_GXApp();
        private V_PLM_CP_WGGX_ZApp v_plm_cp_wggx_zApp = new V_PLM_CP_WGGX_ZApp();
        private V_PLM_CP_WGGX_FApp v_plm_cp_wggx_fApp = new V_PLM_CP_WGGX_FApp();
        private V_PLM_CX_ProductSumApp v_plm_cx_productsumApp = new V_PLM_CX_ProductSumApp();
        private V_PLM_CX_ProductCompareApp v_plm_cx_productcompareApp = new V_PLM_CX_ProductCompareApp();
        private V_PLM_CX_ProductSelectApp v_plm_cx_productselectApp = new V_PLM_CX_ProductSelectApp();
        private PLM_CX_ProductOrderApp plm_cx_productorderApp = new PLM_CX_ProductOrderApp();
        private V_PLM_CP_WGCPApp v_plm_cp_wgcpApp = new V_PLM_CP_WGCPApp();
        private V_CLDE_11_MX_CLDEerialQuotaApp v_clde_11_mx_cldeerialquotaApp = new V_CLDE_11_MX_CLDEerialQuotaApp();
        private V_XLK_21_MX_MatCardListAccoApp v_xlk_21_mx_matcardlistaccoApp = new V_XLK_21_MX_MatCardListAccoApp();
        private XLK_21_MX_MatCardListAccoApp xlk_21_mx_matcardlistaccoApp = new XLK_21_MX_MatCardListAccoApp();        
        private V_GYK_51_JQ_TecGXDataApp v_gyk_51_jq_tecgxdataApp = new V_GYK_51_JQ_TecGXDataApp();
        private V_GYK_41_CY_TecGXDataApp v_gyk_41_cy_tecgxdataApp = new V_GYK_41_CY_TecGXDataApp();
        private V_Mol_11_DA_MoldApp v_mol_11_da_moldApp = new V_Mol_11_DA_MoldApp();
        private V_CLDE_12_GG_CLDEerialQuota_DAApp v_clde_12_gg_cldeerialquota_daApp = new V_CLDE_12_GG_CLDEerialQuota_DAApp();
        private V_ZSJ_11_LJ_ProductionApp v_zsj_11_lj_productionApp = new V_ZSJ_11_LJ_ProductionApp();
        private PLM_DJ_MultiLevelApp plm_dj_multilevelApp = new PLM_DJ_MultiLevelApp();
        private V_PLM_DJ_TREEWGGXJGApp v_plm_dj_treewggxjgApp = new V_PLM_DJ_TREEWGGXJGApp();

        [HttpGet]
        [HandlerAuthorize]
        public ActionResult Structure()
        {
            return View();
        }
        [HttpGet]
        [HandlerAuthorize]
        public ActionResult ChooseParts()
        {
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetPartsJson(Pagination pagination, string keyword)
        {
            var data = new
            {
                rows = plm_cp_wgcpApp.GetList(pagination, keyword),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetPartsJson2(Pagination pagination, string keyword)
        {
            var data = new
            {
                rows = v_zsj_11_lj_productionApp.GetList(pagination, keyword),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }
        [HttpGet]
        [HandlerAuthorize]
        public ActionResult Resource()
        {
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetResourceJson(Pagination pagination, string keyword, string keyword2, string keyword3, string keyword4, string begin, string end)
        {
            var data = new
            {
                rows = plm_cp_vehiclenumberApp.GetList(pagination, keyword, keyword2, keyword3, keyword4, begin, end),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }
        [HttpGet]
        [HandlerAuthorize]
        public ActionResult StampingList()
        {
            var orderList = plm_cx_productorderApp.GetList("");
            ViewBag.orderList = orderList;
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetStampingListJson(Pagination pagination, string number, string name, string assemble, string make)
        {
            var data = new
            {
                rows = v_plm_cx_productsumApp.GetList(pagination, number, name, assemble, make),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetStampingListByOrderJson(Pagination pagination, string number, string name, string assemble, string make, string orderNum)
        {
            var data = new
            {
                rows = v_plm_cx_productsumApp.GetList(pagination, number, name, assemble, make, orderNum),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }
        [HttpGet]
        [HandlerAuthorize]
        public ActionResult UsedAmount()
        {
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetUsedAmountJson(string number)
        {
            var list = v_plm_cx_productsumApp.GetDetailList(number);
            var data = new
            {
                rows = list,
                total = list.Count,
                page = 0,
                records = list.Count
            };
            return Content(data.ToJson());
        }
        [HttpGet]
        [HandlerAuthorize]
        public ActionResult StampingContrast()
        {
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult SetStampingContrastResult(string number1, string number2)
        {
            v_plm_cx_productcompareApp.SetStampingContrastResult(number1, number2);
            return Content("1");
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetStampingContrastJson(Pagination pagination, string number, string type)
        {
            var data = new
            {
                rows = v_plm_cx_productcompareApp.GetList(pagination, number, type),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }
        [HttpGet]
        [HandlerAuthorize]
        public ActionResult SingleQuery()
        {
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetSingleQueryJson(Pagination pagination, string number, string name, string assemble, string make)
        {
            var data = new
            {
                rows = v_plm_cp_wgcp_gxApp.GetList(pagination, number, name, assemble, make),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }
        [HttpGet]
        [HandlerAuthorize]
        public ActionResult SingleQueryNext(string dh)
        {
            ViewBag.dh = dh;
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetSingleNextJson(Pagination pagination, string dh, string number, string name, string assemble, string make)
        {
            var data = new
            {
                rows = v_plm_cp_wggx_zApp.GetList(pagination, dh, number, name, assemble, make),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }
        [HttpGet]
        [HandlerAuthorize]
        public ActionResult SingleQueryPrevious(string dh)
        {
            ViewBag.dh = dh;
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetSinglePreviousJson(Pagination pagination, string dh, string number, string name, string assemble, string make)
        {
            var data = new
            {
                rows = v_plm_cp_wggx_fApp.GetList(pagination, dh, number, name, assemble, make),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }
        [HttpGet]
        [HandlerAuthorize]
        public ActionResult MultiQuery()
        {
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetMultiQueryDetailJson(Pagination pagination, string number)
        {
            var data = new
            {
                rows = v_plm_dj_treewggxjgApp.GetList(pagination, number),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetMultiQueryTopJson()
        {
            return Content(plm_dj_multilevelApp.GetList("").ToJson());
        }
        [HttpGet]
        [HandlerAuthorize]
        public ActionResult MultiQueryCalculate()
        {
            return View();
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitMultiQueryCalculate(string id, string th, string type)
        {
            int result = v_plm_dj_treewggxjgApp.MultiQueryCalculate(id, th, type);
            return Success("计算操作成功。");
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetMultiQueryStructureJson(string id, string text, string value, string checkstate)
        {
            id = HttpUtility.UrlDecode(id);
            text = HttpUtility.UrlDecode(text);
            value = HttpUtility.UrlDecode(value);
            checkstate = HttpUtility.UrlDecode(checkstate);
            List<TreeViewModel> treeList = new List<TreeViewModel>();
            string parent_id = "0";
            List<V_PLM_DJ_TREEWGGXJGEntity> list = v_plm_dj_treewggxjgApp.GetMultiQueryList();
            if (id == "0")
            {
                var list_1 = list.FindAll(s => s.CCBZ == "1");
                var root_id = Guid.NewGuid().ToString();
                treeList.Add(new TreeViewModel() { id = root_id, text = "多级查询 - 结构", value = "1", parentId = parent_id, isexpand = true, complete = true, hasChildren = true });
                foreach (var item in list_1)
                {
                    string content = "";
                    if (item.FSign == "H")
                    {
                        content = string.Format("{6}&rarr;{0}，{1}，{2}，{3}，{4}，{5}", item.WZH, item.TH, item.MC, item.SL, item.StartDate, item.StopDdate, item.FDH);
                    }
                    else
                    {
                        content = string.Format("{0}，{1}，{2}，{3}，{4}，{5}", item.WZH, item.TH, item.MC, item.SL, item.StartDate, item.StopDdate);
                    }
                    int index = list.IndexOf(item);
                    bool hasChildren = false;
                    if(index < list.Count-1 && list[index + 1].CCBZ == "2")
                    {
                        hasChildren = true;
                    }
                    treeList.Add(new TreeViewModel() { id = Guid.NewGuid().ToString(), text = content, value = string.Format("{0},{1}", item.CCBZ, item.TH), parentId = root_id, isexpand = false, complete = false, hasChildren = hasChildren });
                }
            }
            else
            {
                parent_id = id;
                string parent_ccbz = "";
                string parent_th = "";
                string[] valueArray = value.Split(',');
                if (valueArray.Length > 0)
                {
                    parent_ccbz = valueArray[0];
                }
                if (valueArray.Length > 1)
                {
                    parent_th = valueArray[1];
                }
                List<V_PLM_DJ_TREEWGGXJGEntity> list_sub = new List<V_PLM_DJ_TREEWGGXJGEntity>();
                int parent_ccbz_int = 0;
                int.TryParse(parent_ccbz, out parent_ccbz_int);
                int curr_ccbz_int = parent_ccbz_int + 1;
                string curr_ccbz = curr_ccbz_int.ToString();
                switch (parent_ccbz_int)
                { 
                    case 1:
                        list_sub = list.FindAll(s => s.CCBZ == curr_ccbz && s.ZDH01 == parent_th);
                        break;
                    case 2:
                        list_sub = list.FindAll(s => s.CCBZ == curr_ccbz && s.ZDH02 == parent_th);
                        break;
                    case 3:
                        list_sub = list.FindAll(s => s.CCBZ == curr_ccbz && s.ZDH03 == parent_th);
                        break;
                    case 4:
                        list_sub = list.FindAll(s => s.CCBZ == curr_ccbz && s.ZDH04 == parent_th);
                        break;
                    case 5:
                        list_sub = list.FindAll(s => s.CCBZ == curr_ccbz && s.ZDH05 == parent_th);
                        break;
                    case 6:
                        list_sub = list.FindAll(s => s.CCBZ == curr_ccbz && s.ZDH06 == parent_th);
                        break;
                    case 7:
                        list_sub = list.FindAll(s => s.CCBZ == curr_ccbz && s.ZDH07 == parent_th);
                        break;
                    case 8:
                        list_sub = list.FindAll(s => s.CCBZ == curr_ccbz && s.ZDH08 == parent_th);
                        break;
                    case 9:
                        list_sub = list.FindAll(s => s.CCBZ == curr_ccbz && s.ZDH09 == parent_th);
                        break;
                    case 10:
                        list_sub = list.FindAll(s => s.CCBZ == curr_ccbz && s.ZDH10 == parent_th);
                        break;
                    case 11:
                        list_sub = list.FindAll(s => s.CCBZ == curr_ccbz && s.ZDH11 == parent_th);
                        break;
                    case 12:
                        list_sub = list.FindAll(s => s.CCBZ == curr_ccbz && s.ZDH12 == parent_th);
                        break;
                    case 13:
                        list_sub = list.FindAll(s => s.CCBZ == curr_ccbz && s.ZDH13 == parent_th);
                        break;
                    case 14:
                        list_sub = list.FindAll(s => s.CCBZ == curr_ccbz && s.ZDH14 == parent_th);
                        break;
                    default:
                        list_sub = new List<V_PLM_DJ_TREEWGGXJGEntity>();
                        break;
                }
                foreach (var item in list_sub)
                {
                    string content = "";
                    if (item.FSign == "H")
                    {
                        content = string.Format("{6}&rarr;{0}，{1}，{2}，{3}，{4}，{5}", item.WZH, item.TH, item.MC, item.SL, item.StartDate, item.StopDdate, item.FDH);
                    }
                    else
                    {
                        content = string.Format("{0}，{1}，{2}，{3}，{4}，{5}", item.WZH, item.TH, item.MC, item.SL, item.StartDate, item.StopDdate);
                    }
                    int index = list.IndexOf(item);
                    bool hasChildren = false;
                    if (index < list.Count - 1 && list[index + 1].CCBZ == (curr_ccbz_int + 1).ToString())
                    {
                        hasChildren = true;
                    }
                    treeList.Add(new TreeViewModel() { id = Guid.NewGuid().ToString(), text = content, value = string.Format("{0},{1}", item.CCBZ, item.TH), parentId = parent_id, isexpand = false, complete = false, hasChildren = hasChildren });
                }
            }
            return Content(treeList.TreeViewJson(parent_id));
        }
        [HttpGet]
        [HandlerAuthorize]
        public ActionResult MultiQueryStructure()
        {
            return View();
        }

        public ActionResult ArrangeComponent()
        {
            var orderList = plm_cx_productorderApp.GetList("");
            ViewBag.orderList = orderList;
            return View();
        }
        [HttpGet]
        [HandlerAuthorize]
        public ActionResult ArrangeComponentInput()
        {
            return View();
        }
        [HttpGet]
        [HandlerAuthorize]
        public ActionResult ArrangeComponentConfig()
        {
            return View();
        }


        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetArrangeComponentJson(Pagination pagination, string keyword)
        {
            var data = new
            {
                rows = v_plm_cx_productselectApp.GetList(pagination, keyword),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [HandlerAuthorize]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitArrangeComponentInput(string ids)
        {
            List<string> idArray = ids.Split(',').ToList();
            v_plm_cx_productselectApp.ExecProductArrange(idArray);
            return Success("整理产品零件成功。");
        }
        [HttpGet]
        [HandlerAuthorize]
        public ActionResult TechnicalChange()
        {
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetTechnicalChangeJson(Pagination pagination, string keyword, string number, string name, string memo, string begin, string end)
        {
            var data = new
            {
                rows = v_plm_gg_wgggcpApp.GetList(pagination, keyword, number, name, memo, begin, end),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetPartsItemJson(string dh)
        {
            var data = v_plm_cp_wgcp_gxApp.GetItem(dh);
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetBomDetailJson(string id)
        { 
            string[] array = id.Split(',');
            string zhd = array[0];
            string fdh = array.Length > 0 ? array[1] : "0";
            fdh = fdh == "0" ? "" : fdh;
            var item1 = v_plm_cp_wgcpApp.GetItem(zhd, fdh);
            var item2 = v_clde_11_mx_cldeerialquotaApp.GetItemByProduction(zhd);
            var item = new { item1, item2 };
            return Content(item.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetCldeAllListJson(Pagination pagination, string FProduction, string MateAlterTecNO)
        {
            var data = new
            {
                rows = v_clde_11_mx_cldeerialquotaApp.GetList(pagination, FProduction, MateAlterTecNO),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetCldeDocumentListJson(Pagination pagination, string FProduction, string MateAlterTecNO)
        {
            var data = new
            {
                rows = v_clde_12_gg_cldeerialquota_daApp.GetList(pagination, FProduction, MateAlterTecNO),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetJqxlListJson(string id)
        {
            id = id == null ? "" : id;
            string[] array = id.Split(',');
            string zhd = array[0];
            var list = v_xlk_21_mx_matcardlistaccoApp.GetList(zhd);
            return Content(list.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetJqxlAllListJson(Pagination pagination, string status, string keyword)
        {
            var data = new
            {
                rows = v_xlk_21_mx_matcardlistaccoApp.GetList(pagination, status, keyword),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [HandlerAuthorize]
        [ValidateAntiForgeryToken]
        public ActionResult SetJqxlItemChecked(string keyValue)
        {
            XLK_21_MX_MatCardListAccoEntity entity = new XLK_21_MX_MatCardListAccoEntity();
            entity.MatListID = keyValue;
            entity.MaintainStatus = "1";
            xlk_21_mx_matcardlistaccoApp.SubmitForm(entity, keyValue);
            return Success("审核成功。");
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetJqgxListJson(Pagination pagination, string id)
        {
            id = id == null ? "" : id;
            string[] array = id.Split(',');
            string zhd = array[0];
            var list = v_gyk_51_jq_tecgxdataApp.GetListByProduction(pagination, zhd);
            return Content(list.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetCygxListJson(Pagination pagination, string id)
        {
            id = id == null ? "" : id;
            string[] array = id.Split(',');
            string zhd = array[0];
            var list = v_gyk_41_cy_tecgxdataApp.GetListByProduction(pagination, zhd);
            return Content(list.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetModlItemJson(string id)
        {
            id = id == null ? "" : id;
            var list = v_mol_11_da_moldApp.GetItem(id);
            return Content(list.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetBomTreeJson(bool? if_parent, string id, string text, string value, string checkstate)
        {
            List<TreeViewModel> treeList = new List<TreeViewModel>();
            string parent_id = "0";
            if (if_parent.HasValue && if_parent == true)
            {
                var root = v_plm_cp_wggxApp.GetItem(id);
                if (root != null)
                {
                    treeList.Add(new TreeViewModel() { id = root.ZDH, text = string.Format("{0}，{1}，{2}，{3}", root.WZH, root.ZDH, root.SL, root.ZMC), value = root.ZDH + "," + parent_id, parentId = parent_id, isexpand = true, complete = true, hasChildren = (root.SubCount > 0) });
                }
                else
                {
                    treeList.Add(new TreeViewModel() { id = id, text = id, value = id + "," + parent_id, parentId = parent_id, isexpand = true, complete = true, hasChildren = true });
                }
            }
            else
            {
                parent_id = id;
            }
            var list = v_plm_cp_wggxApp.GetList(id);
            foreach (var item in list)
            {
                treeList.Add(new TreeViewModel() { id = item.ZDH, text = string.Format("{0}，{1}，{2}，{3}", item.WZH, item.ZDH, item.SL, item.ZMC), value = item.ZDH + "," + item.FDH, parentId = item.FDH, isexpand = false, complete = false, hasChildren = (item.SubCount > 0) });
            }
            return Content(treeList.TreeViewJson(parent_id));
        }
    }
}
